package com.ruoyi.web.controller.platform;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.platform.domain.MStoreLocation;
import com.ruoyi.platform.exception.BusinessException;
import com.ruoyi.platform.service.IMExecuteCommandResultService;
import com.ruoyi.platform.service.IMMonitoringEquipmentService;
import com.ruoyi.platform.service.IMStoreLocationService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.platform.domain.MExecuteCommand;
import com.ruoyi.platform.service.IMExecuteCommandService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 【执行指令】Controller
 *
 * @author dataControl
 * @date 2025-09-15
 */
@RestController
@RequestMapping("/platform/command")
public class MExecuteCommandController extends BaseController {
    @Autowired
    private IMExecuteCommandService mExecuteCommandService;

    @Autowired
    private IMExecuteCommandResultService commandResultService;

    @Autowired
    private IMStoreLocationService locationService;

    @Autowired
    private IMMonitoringEquipmentService equipmentService;

    /**
     * 查询【执行指令】列表
     */
    @PreAuthorize("@ss.hasPermi('platform:command:list')")
    @GetMapping("/list")
    public TableDataInfo list(MExecuteCommand mExecuteCommand) {
        startPage();
        List<MExecuteCommand> list = mExecuteCommandService.selectMExecuteCommandList(mExecuteCommand);
        return getDataTable(list);
    }

    /**
     * 导出【执行指令】列表
     */
    @PreAuthorize("@ss.hasPermi('platform:command:export')")
    @Log(title = "导出【执行指令】列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MExecuteCommand mExecuteCommand) {
        List<MExecuteCommand> list = mExecuteCommandService.selectMExecuteCommandList(mExecuteCommand);
        ExcelUtil<MExecuteCommand> util = new ExcelUtil<MExecuteCommand>(MExecuteCommand.class);
        util.exportExcel(response, list, "【执行指令】数据");
    }

    /**
     * 获取【执行指令】详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:command:query')")
    @GetMapping(value = "/{commandId}")
    public AjaxResult getInfo(@PathVariable("commandId") Long commandId) {
        MExecuteCommand item = mExecuteCommandService.selectMExecuteCommandByCommandId(commandId);
        if (item != null) {
            item.setExecuteCommandResult(commandResultService.selectMExecuteCommandResultByCommandId(item.getCommandId()));
            MStoreLocation location = locationService.selectMStoreLocationByStoreLocationId(item.getStoreLocationId());
            item.setLocation(location);
            if (location != null) {
                item.setEquipment(equipmentService.selectMMonitoringEquipmentByEquipmentId(location.getEquipmentId()));
            }
        }
        return success(item);
    }

    /**
     * 获取【执行指令】详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:command:query')")
    @GetMapping(value = "/getResultCommand")
    public AjaxResult getCommand(@RequestParam(value = "commandId", required = true) Long commandId,
                                 @RequestParam(value = "commandResultId", required = true) Long commandResultId) {
        MExecuteCommand item = mExecuteCommandService.selectMExecuteCommandByCommandId(commandId);
        if (item != null) {
            item.setExecuteCommandResult(commandResultService.selectMExecuteCommandResultByCommandResultId(commandResultId));
            MStoreLocation location = locationService.selectMStoreLocationByStoreLocationId(item.getStoreLocationId());
            item.setLocation(location);
            if (location != null) {
                item.setEquipment(equipmentService.selectMMonitoringEquipmentByEquipmentId(location.getEquipmentId()));
            }
        }
        return success(item);
    }

    /**
     * 重试【执行指令】详细信息
     */
    @PreAuthorize("@ss.hasPermi('platform:command:query')")
    @Log(title = "重试【执行指令】", businessType = BusinessType.INSERT)
    @GetMapping(value = "/retrySend")
    public AjaxResult retrySend(@RequestParam(value = "commandId", required = true) Long commandId,
                                @RequestParam(value = "commandResultId", required = true) Long commandResultId) {
        try {
            return toAjax(mExecuteCommandService.retrySendCommandByCommandId(commandId, commandResultId));
        } catch (BusinessException ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    /**
     * 新增【执行指令】
     */
    @PreAuthorize("@ss.hasPermi('platform:command:add')")
    @Log(title = "新增【执行指令】", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MExecuteCommand mExecuteCommand) {
        try {
            return toAjax(mExecuteCommandService.insertMExecuteCommand(mExecuteCommand));
        } catch (BusinessException ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    /**
     * 修改【执行指令】
     */
    @PreAuthorize("@ss.hasPermi('platform:command:edit')")
    @Log(title = "修改【执行指令】", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MExecuteCommand mExecuteCommand) {
        return toAjax(mExecuteCommandService.updateMExecuteCommand(mExecuteCommand));
    }

    /**
     * 删除【执行指令】
     */
    @PreAuthorize("@ss.hasPermi('platform:command:remove')")
    @Log(title = "删除【执行指令】", businessType = BusinessType.DELETE)
    @DeleteMapping("/{commandIds}")
    public AjaxResult remove(@PathVariable Long[] commandIds) {
        return toAjax(mExecuteCommandService.deleteMExecuteCommandByCommandIds(commandIds));
    }
}
